<template>
  <div class="container" ref="container">
    <Nav></Nav>
    <router-view class="router" ></router-view>
  </div>
</template>

<script setup>
import Nav from '@/components/BusinessNav/index.vue'
import { computed, nextTick, onBeforeMount, onBeforeUpdate, onMounted, provide, reactive, ref } from 'vue'
import { reqOperations } from '../../api/operation';
import { useRoute, useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import useUserStore from '../../store/user';
import useOperationStore from '../../store/operation';
import { replaceRoute } from '@/utils/routerUtil'
import eventBus from '@/eventBus.js'
let container = ref()

let checkOperationInfo = computed(() => {
  if (checkOperation.value == null) return null
  return operationList[checkOperation.value]
})
let operationStore = useOperationStore()
console.log(operationStore.index);

if(operationStore.index == null) operationStore.updateCheck(0)


// 选中业务id
let checkOperation = ref(operationStore.index)
let operationId = ref(operationStore.data.length > 0 ? operationStore.data[operationStore.index].id : '')
let operationList = ref(operationStore.data)
operationStore.$subscribe(async (mutation, state) => {
  checkOperation.value = operationStore.index
  operationId.value = operationStore.data[operationStore.index].id
  operationList.value = operationStore.data
})

let $route = useRoute()
let $router = useRouter()
let userStore = useUserStore()

const initData = async() => {
  if (!$route.query.companyId) {
    ElMessage({
      type: "warning",
      message: "无具体公司信息"
    })
    setTimeout(() => { $router.push('/home') }, 1000)
    return
  }
  userStore.updateVISIT($route.query.companyId)
  let res = await reqOperations($route.query.companyId)
  // code 0:成功引入，1：token过期，2：其它错误
  if(res.code == 0){
    operationStore.updateData(res.data)
  }
}
onMounted(async() => {
  eventBus.on('updateBusinessRoutes', () => {
    const currentPath = $router.currentRoute.value.fullPath;
    $router.push(currentPath).catch(() => {});
  });
  container.value.style.height = screen.height * 0.8 + 'px'
  container.value.style.width = screen.width * 0.8 + 'px'
  if ($route.query.operationIdx) {
    checkOperation.value=$route.query.operationIdx
  }
  await initData()
  replaceRoute()
  
})
// 提供子组件信息
provide("operation", checkOperationInfo)
</script>

<style lang="scss" scoped>
.container {
  box-sizing: border-box;
  background-color: rgb(238, 233, 233);
  flex-direction: column;
  min-width: 100%;
  min-height: 100%;
  height: 1080px;
  width: 1920px;
  display: flex;
  align-items: center;

  .router {
    display: flex;
    flex: 1;
    overflow: hidden;
  }
}
</style>